//
//  TransactionRecordsVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "TransactionRecordsVC.h"
#import "MJRefresh.h"
#import "TransactionRecordsCell.h"
#import "MyVM.h"
#import "MJRefresh.h"
@interface TransactionRecordsVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;

@property (nonatomic ,assign)NSInteger page;


@end

@implementation TransactionRecordsVC{
    UIView *lineView;
    UIButton *btnTopUp;
    UIButton *btnConsumption;
    UIButton *btnMakeMoney;
    NSInteger type;///1=充值 2=消费 3=赚钱
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"交易记录";
    [self headView];
    type=1;
    [self.tableView.header beginRefreshing];
}

- (void)backClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)getData{
    WS(weakSelf);
    [[MyVM manage] getRecordListWithType:type andPage:self.page andSuccess:^(id returnValue) {
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView.header endRefreshing];
        weakSelf.page+=1;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
    }];
}

- (void)headView{
    WS(weakSelf);
    UIView *topView=[[UIView alloc]init];
    topView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf.view);
        make.height.mas_equalTo(@39);
    }];
    
    btnTopUp=[self createBtnWithTitle:@"充值记录"];
    btnTopUp.tag=1;
    [topView addSubview:btnTopUp];
    [btnTopUp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@34);
    }];
    
    btnConsumption=[self createBtnWithTitle:@"消费记录"];
    btnConsumption.tag=2;
    [topView addSubview:btnConsumption];
    [btnConsumption mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView);
        make.centerX.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@34);
    }];
    
    btnMakeMoney=[self createBtnWithTitle:@"赚钱记录"];
    btnMakeMoney.tag=3;
    [topView addSubview:btnMakeMoney];
    [btnMakeMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView);
        make.right.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
        make.height.mas_equalTo(@34);
    }];
    
    lineView=[[UIView alloc]init];
    lineView.backgroundColor=NAVCOLOR;
    [topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnTopUp);
        make.bottom.equalTo(topView);
        make.size.mas_equalTo(CGSizeMake(44, 2));
    }];
}

- (UIButton *)createBtnWithTitle:(NSString *)title{
    UIButton *btn=[[UIButton alloc]init];
    [btn addTarget:self action:@selector(onclickTop:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    return btn;
}


- (void)onclickTop:(UIButton *)sender{
    type=sender.tag;
    [self.tableView.header beginRefreshing];
    if (sender.tag==1) {
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(sender);
        }];
    }else if (sender.tag==2){
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnTopUp).with.offset(SCREEN_WIDTH/3.0);
        }];
    }else if (sender.tag==3){
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnTopUp).with.offset(SCREEN_WIDTH/3.0*2);
        }];
    }
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    view.backgroundColor=DEFAULTBGCOLOR;
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TransactionRecordsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TransactionRecordsCell" forIndexPath:indexPath];
    cell.recordsModel=self.dataArr[indexPath.row];
    return cell;
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        [_tableView registerClass:[TransactionRecordsCell class] forCellReuseIdentifier:@"TransactionRecordsCell"];
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight=70;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            weakSelf.page=1;
            [weakSelf getData];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData];
        }];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view).with.
            insets(UIEdgeInsetsMake(39, 0, 0, 0));
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
